/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import ru.rantipin.persistence.PersistenceHelper;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.JOptionPane;

/**
 *
 * @author rantipin
 */
public class ResearchPanel extends JPanel{
    
    public ResearchPanel(DataBaseElementPanel film, DataBaseElementPanel calander) {
        formulaPanel = film;
        calanderPanel = calander;
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        
        gridbag.setConstraints(getCreateNewValues(), 
                new GridBagConstraints(
                    0, 0,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 10, 0, 10),
                    0, 0
                ));
        add(getCreateNewValues());
        
        gridbag.setConstraints(getPreserveValues(), 
                new GridBagConstraints(
                    0, 1,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getPreserveValues());
        
        gridbag.setConstraints(getNameText(), 
                new GridBagConstraints(
                    0, 2,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getNameText());
        
        gridbag.setConstraints(getCalendar(), 
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    10, 0
                ));
        add(getCalendar());
        
        gridbag.setConstraints(getTime(), 
                new GridBagConstraints(
                    1, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getTime());
        
        gridbag.setConstraints(getDescriptionLabel(), 
                new GridBagConstraints(
                    0, 4,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getDescriptionLabel());
        
        JScrollPane area = new JScrollPane(getTextArea());
        area.setMinimumSize(new Dimension(0, 60));
        area.setPreferredSize(new Dimension(0, 60));
        gridbag.setConstraints(area, 
                new GridBagConstraints(
                    0, 5,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 1.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(area);
        
        gridbag.setConstraints(getSaveButton(), 
                new GridBagConstraints(
                    0, 6,
                    1, 1,
                    1.0, 1.0,
                    GridBagConstraints.LAST_LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 0, 10, 10),
                    0, 0
                ));
        add(getSaveButton());
        
        gridbag.setConstraints(getCancelButton(), 
                new GridBagConstraints(
                    1, 6,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LAST_LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 0, 10, 10),
                    0, 0
                ));
        add(getCancelButton());       
    }
    
    private JCheckBox getCreateNewValues() {
        if (createNewValues == null) {
            createNewValues = new JCheckBox("Create new research", false);
            createNewValues.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    boolean selected = createNewValues.isSelected();
                    clearResearchPanel(selected);
                    if(selected) {
                        calanderPanel.clearPanel();
                        calanderPanel.setPreserveValues();
                        formulaPanel.clearPanel();
                        formulaPanel.setPreserveValues();
                    } else {
                        calanderPanel.clearPanel();
                        formulaPanel.clearPanel();
                        initialize();
                    }
                }
            });
        }        
        return createNewValues;
    }
    
    private void clearResearchPanel(boolean marker) {
        saveButton.setVisible(marker);
        cancelButton.setVisible(marker);
        nameText.setText("");
        nameText.setEditable(marker);
        nameText.setVisible(marker);
        preserveValues.removeAllItems();
        preserveValues.setVisible(!marker);
        discriptionArea.setText("");
        discriptionArea.setEditable(marker);
        date.setEnabled(marker);
        time.setEnabled(marker);
    }
    
    private JComboBox getPreserveValues() {
        if (preserveValues == null) {
            preserveValues = new JComboBox();
            preserveValues.setEditable(false);
            preserveValues.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
//                    JComboBox combobox = (JComboBox)e.getSource();
//                    Object selectedItem = combobox.getSelectedItem();
//                    if (selectedItem != null) {
//                        Research research = PersistenceHelper.getInstance().getResearchData(selectedItem);
//                        nameText.setVisible(false);
//                        discriptionArea.setText(research.getDescription());
//                        date.setEnabled(false);
//                        date.setDate(research.getDate());
//                        time.setValue(research.getDate());
//                        calanderPanel.setCalanderPanelValues(research);
//                        formulaPanel.setFormulaPanelValues(research);
//                    }
                    }
            });
        }
        return preserveValues;
    }

    public void initialize() {
//        ArrayList temp = PersistenceHelper.getInstance().getResearchNames();
//        for (Object obj: temp) {
//            preserveValues.addItem(obj);
//        }
    }
    
    private JTextField getNameText() {
        if (nameText == null) {
            nameText = new JTextField();
            nameText.setEditable(false);
        }        
        return nameText;
    }
    
    private JLabel getDescriptionLabel() {
        if (descriptionLabel == null) {
                descriptionLabel = new JLabel("Description:");
        }        
        return descriptionLabel;
    }

    private JTextArea getTextArea() {
        if (discriptionArea == null) {
                discriptionArea = new JTextArea();
                discriptionArea.setLineWrap(true);
                discriptionArea.setWrapStyleWord(true);
                discriptionArea.setEditable(false); 
        }        
        return discriptionArea;
    }
    
    private JDateChooser getCalendar() {
        if (date == null) {
            date = new JDateChooser("dd.MM.yyyy", "##.##.####", '_');
            date.setDate(new Date());
            date.setEnabled(false);
        }
        
        return date;
    }
    
    private JSpinner getTime() {
        if (time == null) {
            SpinnerDateModel model = new SpinnerDateModel();
            time = new JSpinner(model);
            time.setEditor(new JSpinner.DateEditor(time, "HH:mm:ss"));
            time.setEnabled(false);
        }
        return time;
    }
    
    private JButton getCancelButton() {
        if (cancelButton == null) {
            cancelButton = new JButton("Cancel");
            cancelButton.setVisible(false);
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    clearResearchPanel(false);
                    formulaPanel.clearPanel();
                    calanderPanel.clearPanel();
                    createNewValues.setSelected(false);
                    initialize();
                }
            });
        }
        return cancelButton;
    }
    
    private JButton getSaveButton() {
        if (saveButton == null) {
            saveButton = new JButton("Save");
            saveButton.setVisible(false);
            saveButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
//                    Research research = new Research();
//                    research.setName(nameText.getText());
//                    research.setDate(date.getDate());
//                    research.setDescription(discriptionArea.getText());
//                    research.setIdFormula(PersistenceHelper.getInstance().getFormulaData(formulaPanel.getComboboxObject()));
//                    research.setIdCalander(PersistenceHelper.getInstance().getCalanderData(calanderPanel.getComboboxObject()));
//                    research.setParameterCollection(calanderPanel.getParametersFromTable());
//                    PersistenceHelper.getInstance().setNewResearch(research);
//                    JOptionPane.showMessageDialog(ResearchPanel.this, "Data was successful saved",
//                                  "Data Saved", JOptionPane.OK_OPTION);
                }
            });
        }
        
        return saveButton;
    }
    
   
    private JCheckBox createNewValues = null;
    private JComboBox preserveValues = null;
    private JTextField nameText = null;
    private JDateChooser date = null;
    private JSpinner time = null;
    private JLabel descriptionLabel = null;
    private JTextArea discriptionArea = null;
    private JButton saveButton = null;
    private JButton cancelButton = null;
    private DataBaseElementPanel formulaPanel = null;
    private DataBaseElementPanel calanderPanel = null;    
}
